import { list, tree } from '../services/sum';

export default {
  namespace: 'sum',

  state: {
    data: [],
    exportData: [],

    counts: {
      total: 0,
      lodging1sex1: 0, // 住校男生人数
      lodging1sex0: 0, // 住校女生人数
      lodging0sex1: 0, // 跑堂男生人数
      lodging0sex0: 0, // 跑堂女生人数
    },

    treeData: [],
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(list, payload);
      yield put({ type: 'callbackList', response });
    },
    *listExport({ payload }, { call, put }) {
      const response = yield call(list, payload);
      yield put({ type: 'callbackList', response, isExport: true });
    },
    *tree({ payload }, { call, put }) {
      const response = yield call(tree, payload);
      yield put({ type: 'callbackTree', response });
    },
  },

  reducers: {
    callbackList(st, { response, isExport }) {
      const state = st;
      if (response.success) {
        state.counts = response.counts;
        if (isExport) state.exportData = response.data;
        else state.data = response.data;
      }
      return { ...state };
    },
    callbackTree(st, { response }) {
      const state = st;
      if (response.success) {
        state.treeData = response.data;
      }
      return { ...state };
    },
  },
};
